require "common"

function get_distinct_num( num )
	local dist = {}
	local str = {}
	local out = ""
	local len = string.len( num )
	str = sort_str_gxkl10(num)
	for i=1,len ,2 do
		c = string.sub(str,i,i+1)
		if not in_table( c, dist ) then
			table.insert( dist, c )
		end
	end
	for i = 1,table.getn(dist) do
		out = out..dist[i]
	end
	return	out , table.getn(dist)

end

--判断字符串
function judge_str( num , max)
	local dist
	local out = ""
	local st = {}
	if num == nil then return nil end
	local len = string.len( num )
	if len % 2 ~= 0 then return nil end

	for i=1,len,2 do
			c = string.sub(num,i,i+1)
			if tonumber(c) > max or tonumber(c) < 1 then return nil end

	end

	_,dist = get_distinct_num(num)
	if dist ~= string.len(num) /2 then return nil end

	return true
end

--数字字符串排序

function sort_str_gxkl10( num )
	num = string.gsub( num, "*", "")
	local out = ""
	local st = {}
	local len = string.len( num )
	for i=1,len,2 do
		table.insert( st, string.sub(num,i,i+1) )
	end
	table.sort(st)
	for i=1,len / 2 do
		out = out..st[i]
	end

	return out

end

function judge_num(str, min, max)

	if string.len(str) / 2 < min or string.len(str) / 2 > max then return nil end

	return true
end

function check_gxkl10(butType, inStr)
	local qxStr;
	local qjStr;
	local butNum = 0;
	local butPower = 1;
	local butStr = "";
	local itemList = {}
	local itemLen = 0;
	local numNum

	itemList = Split( inStr, "#B" )
	itemLen = table.getn( itemList )

	qjStr = string.gsub( inStr, "#B", "0")
	qxStr = string.gsub( qjStr, "#", "0")
	if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then return 0 end

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	if butPower >99 or butPower < 1 then return 0 end

	if not judge_str(itemList[1], 21) then return 0 end

	numNum = string.len(itemList[1]) / 2
	--直选好运特
	if butType == 'a' then
		if not judge_num(itemList[1], 1, 9) then return 0 end
		butNum = combination(numNum, 1)
	--直选好运一 或者
	elseif butType == '1' then
		if not judge_num(itemList[1], 1, 2) then return 0 end
		butNum = combination(numNum, 1)
	--直选好运二
	elseif butType == '2' then
		if not judge_num(itemList[1], 2, 5) then return 0 end
		butNum = combination(numNum, 2)
	--直选好运3
	elseif butType == '3' then
		if not judge_num(itemList[1], 3, 8) then return 0 end
		butNum = combination(numNum, 3)
	--直选好运4
	elseif butType == '4' then
		if not judge_num(itemList[1], 4, 12) then return 0 end
		butNum = combination(numNum, 4)
	--直选好运5
	elseif butType == '5' then
		if not judge_num(itemList[1], 5, 14) then return 0 end
		butNum = combination(numNum, 5)
	--通选好运3
	elseif butType == '6' then
		if not judge_num(itemList[1], 3, 6) then return 0 end
		butNum = combination(numNum, 3)
	--通选好运4
	elseif butType == '7' then
		if not judge_num(itemList[1], 4, 10) then return 0 end
		butNum = combination(numNum, 4)
	--通选好运5
	elseif butType == '8' then
		if not judge_num(itemList[1], 5, 14) then return 0 end
		butNum = combination(numNum, 5)
	else
		return 0
	end


	return butNum, butPower, inStr, inStr
end

function query_gxkl10(butType, inStr, djStr)
	local nBingo = {0,0,0,0,0,0,0,0,0,0,0,0,0,0}
	local temp 	 = {0,0,0,0,0,0,0,0,0,0,0,0,0}
	local butPower = 1
	local numNum

	itemList = Split( inStr, "#B" )
	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end

	numNum = string.len(itemList[1]) / 2

	if butType == 'a' then
		if string.find(inStr,string.sub(djStr, 9, 10)) ~= nil then
			nBingo[1] = nBingo[1] + 1
		end
	elseif butType == '1' or butType == '2' or butType == '3' or butType == '4' or butType == '5' then
		butType = string.byte(butType) - string.byte('0')
		bingoNum = ConputeBingoNum(itemList[1], djStr, 2)
		if butType <= 5 then
			nBingo[butType + 1] = combination(bingoNum, butType)
		else
			return 0
		end
	elseif butType == '6' then
		bingoNum = ConputeBingoNum(itemList[1], djStr, 2)
		if bingoNum == 3 then
			nBingo[7] = combination(bingoNum, 3)
			if numNum > 3 then
				nBingo[8] = (numNum - 3) * 3
			end
		elseif bingoNum == 2 then
			nBingo[8] = numNum - 2
		end
	elseif butType == '7' then
		bingoNum = ConputeBingoNum(itemList[1], djStr, 2)
		if bingoNum == 4 then
			nBingo[9] = combination(bingoNum, 4)
			if numNum > 4 then
				nBingo[10] = (numNum - 4) * 4
				nBingo[11] = combination(numNum - 4, 2) * 6
			end
		elseif bingoNum == 3 then
			nBingo[10] = numNum - 3
			if numNum > 4 then
				nBingo[11] = combination(numNum - 3, 2) * 3
			end
		elseif bingoNum == 2 then
			nBingo[11] = combination(numNum - 2, 2)
		end
	elseif butType == '8' then
		bingoNum = ConputeBingoNum(itemList[1], djStr, 2)
		if bingoNum == 5 then
			nBingo[12] = combination(bingoNum, 5)
			if numNum > 5 then
				nBingo[13] = (numNum - 5) * 5
				nBingo[14] = combination(numNum - 5, 2) * 10
			end
		elseif bingoNum == 4 then
			nBingo[13] = numNum - 4
			if numNum > 5 then
				nBingo[14] = combination(numNum - 4, 2) * 4
			end
		elseif bingoNum == 3 then
			nBingo[14] = combination(numNum - 3, 2)
		end
	else
		return 0
	end

	for i=1 ,14 do
		nBingo[i] = nBingo[i] * butPower
	end
	return nBingo[1]..","..nBingo[2]..","..nBingo[3]..","..nBingo[4]..","..nBingo[5]..","..nBingo[6]..","..nBingo[7]..","..nBingo[8]..","..nBingo[9]..","..nBingo[10]..","..nBingo[11]..","..nBingo[12]..","..nBingo[13]..","..nBingo[14]
end



--print(check_gxkl10('a',"0802#B10"))
--print(check_gxkl10('3',"012109#B10"))
--print(check_gxkl10('4',"012109121314#B10"))
--print(check_gxkl10('6',"012109121314#B10"))
--print(check_gxkl10('7',"01210912131411151617#B10"))
--print(check_gxkl10('8',"01210912131411151617181921#B10"))
--print(query_gxkl10('0',"01210905#B10","0102030405"))
--print(query_gxkl10('1',"01210905#B10","0102030405"))
--print(query_gxkl10('8',"01020306070809","0102030405"))
